using System;
using System.ComponentModel.DataAnnotations;
using Coderoom.ScrumPlanner.Domain;
using Coderoom.ScrumPlanner.Domain.Validation;
using Coderoom.ScrumPlanner.Tests.Unit.Internal;
using Xunit;

namespace Coderoom.ScrumPlanner.Tests.Unit.ContainerTests
{
	public class TaskTests
	{
		[Fact]
		public void EstimatedComplexityProperty_is_marked_range_from_0_to_13()
		{
			// arrange
			var attributes = AttributeTestHelper.GetCustomPropertyAttributes<Task, RangeAttribute>("EstimatedComplexity");

			// assert
			Assert.NotNull(attributes);
			Assert.Equal(1, attributes.Length);
			Assert.Equal(0, attributes[0].Minimum);
			Assert.Equal(13, attributes[0].Maximum);
			Assert.Equal("Complexity may only contain values ranging from 0 to 13.", attributes[0].ErrorMessageResourceType.GetString(attributes[0].ErrorMessageResourceName));
		}

		[Fact]
		public void Validation_passes_when_container_is_proper()
		{
			// arrange
			var container = new Task();
			container.EstimatedComplexity = 1;
			container.Title = "title";
			container.Slug = "slug";

			// act
			var result = EntityValidator.Validate(container);

			// assert
			Assert.IsType<ValidationResult>(result);
			Assert.True(result.IsValid);
			Assert.Equal(0, result.Errors.Count);
		}

		[Fact]
		public void Validation_fails_when_EstimatedComplexity_is_14()
		{
			// arrange
			var container = new Task();
			container.UserStoryId = Guid.NewGuid();
			container.EstimatedComplexity = 14;
			container.Title = "title";
			container.Slug = "slug";

			// act
			var result = EntityValidator.Validate(container);

			// assert
			Assert.IsType<ValidationResult>(result);
			Assert.False(result.IsValid);
			Assert.Equal(1, result.Errors.Count);
			Assert.Equal("EstimatedComplexity", result.Errors[0].PropertyName);
			Assert.Equal("Complexity may only contain values ranging from 0 to 13.", result.Errors[0].Message);
			Assert.Equal(container, result.Errors[0].ValidationTarget);
		}
	}
}